home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Scan / 64-bit_x64 / Russian / cpsimage.cab / data / sys / FileVerifier.elf < prev    next >
Text File  |  2009-04-23  |  5KB  |  159 lines

  1. #load "sys/datetime.elf";
  2. #load "sys/stdio.elf";
  3. #load "sys/xipxml.elf";
  4.  
  5. /****************************************************************************/
  6. /*
  7. ** This class provides the means to verify the integrity of a file by
  8. ** computing a MD5 hash value of the file and comparing that value to the
  9. ** corresponding file hash value maintained in a xml database file.
  10. */
  11. /* @New
  12. // DESCRIPTION
  13.      Creates a new instance of FileVerifier for file verification.
  14.  
  15. // ARGUMENTS
  16.      FILE   dbfile      xml database file.
  17.  
  18. // EXAMPLES
  19.      // Example 1: Create a FileVerifier object with empty database
  20.      FileVerifier fv = new (FileVerifier);
  21.  
  22.      //-------------------------------------------------------
  23.  
  24.      // Example 2: Create a FileVerifier object with existing database
  25.      FILE file = new (FILE, path: "testdb.xml");
  26.      FileVerifier fv = new (FileVerifier, dbfile: file);
  27. */
  28. /* @isVerified Indicates if the integrity of a file is verified. */
  29. /* @isDBModified Indicates if the xml database has been changed. */
  30. /* @writeDBFile Writes the xml database to disk using specified directory. */
  31. /****************************************************************************/
  32. CLASS FileVerifier {
  33.    XmlDocument xmldb;
  34.    LIST        indexdb;
  35.    BOOLEAN     dbModified;
  36.  
  37.    METHOD New (FILE dbfile) {
  38.       if (!dbfile) {
  39.          this.xmldb = XmlDocumentBuilder.newDocument ();
  40.          XmlElement root = this.xmldb.createElement (name: "FileVerification");
  41.          this.xmldb.setDocumentElement (node: root);
  42.          }
  43.       else {
  44.          if (!dbfile.exists ())
  45.             SetStatus (op: "Stop", msg: "FileNotFoundException\n");
  46.  
  47.          if (!dbfile.isFile ())
  48.             SetStatus (op: "Stop", msg: "IllegalArgumentException\n");
  49.  
  50.          STRING path = dbfile.getCanonicalPath ();
  51.          this.xmldb = XmlDocumentBuilder.parseFile (filename: path);
  52.  
  53.          XmlElement root = this.xmldb.getDocumentElement ();
  54.          XmlNode    n;
  55.          INTEGER    nentries = 0;
  56.          STRING     name;
  57.          for (n = root.getFirstChild (); n; n = n.getNextSibling ()) {
  58.             if (n.getNodeType () != XmlNode.XML_ELEMENT_NODE)
  59.                continue;
  60.  
  61.             name = n.getAttribute (name: "name");
  62.             this.indexdb.insert (entry: nentries, name: name, obj: n);
  63.             nentries++;
  64.             }
  65.          }
  66.    }
  67.  
  68.    METHOD isVerified (FILE file)
  69.      RETURNS (BOOLEAN verified) {
  70.       if (!file)
  71.          SetStatus (op: "Stop", msg: "NullObjectException\n");
  72.  
  73.       if (!file.exists ())
  74.          SetStatus (op: "Stop", msg: "FileNotFoundException\n");
  75.  
  76.       if (!file.isFile ())
  77.          SetStatus (op: "Stop", msg: "IllegalArgumentException\n");
  78.  
  79.       STRING  name    = file.getName ();
  80.       STRING  path    = file.getCanonicalPath ();
  81.       STRING  actHash =  MD5 (filename: path);
  82.       INTEGER idx     = this.findEntry (name: name);
  83.       if (idx < 0) {
  84.          // Input file does not exists (new)
  85.          this.addEntry (idx: -idx - 1, name: name, hash: actHash);
  86.          this.dbModified = TRUE;
  87.          }
  88.       else {
  89.          // Input file exists
  90.          XmlElement e = this.indexdb[idx];
  91.          STRING expHash = e.getAttribute (name: "hash");
  92.          if (actHash == expHash) {
  93.             // Input file is verified (hash values match)
  94.             verified = TRUE;
  95.             }
  96.          else {
  97.             // Input file is not verified (hash values differ)
  98.             e.setAttribute (name: "hash", value: actHash);
  99.             this.dbModified = TRUE;
  100.             }
  101.          }
  102.    }
  103.  
  104.    METHOD isDBModified ()
  105.      RETURNS (BOOLEAN mod) {
  106.       mod = this.dbModified;
  107.    }
  108.  
  109.    METHOD writeDBFile (FILE dir) {
  110.       DATETIME dt = DATETIME.now (utc: FALSE);
  111.  
  112.       STRING dbpath = "db" + dt.toString () + ".xml";
  113.       if (dir) {
  114.          if (!dir.exists ())
  115.             dir.mkdirs ();
  116.          else if (!dir.isDir ())
  117.             SetStatus (op: "Stop", msg: "IllegalArgumentException\n");
  118.  
  119.          dbpath = dir.getCanonicalPath () + FILE.sep () + dbpath;
  120.          }
  121.  
  122.       this.xmldb.dumpFile (filename: dbpath, format: 1);
  123.    }
  124.  
  125.    private METHOD findEntry (STRING name)
  126.      RETURNS (INTEGER idx) {
  127.       INTEGER low = 0, high = this.indexdb.length () - 1;
  128.       STRING  curName;
  129.       while (low <= high) {
  130.          idx = (low + high) >> 1;
  131.  
  132.          curName = this.indexdb.name (entry: idx);
  133.          if (curName == name)
  134.             return;
  135.  
  136.          if (curName < name)
  137.             low = idx + 1;
  138.          else
  139.             high = idx - 1;
  140.          }
  141.       idx = -(low + 1);
  142.    }
  143.  
  144.    private METHOD addEntry (INTEGER idx, STRING name, STRING hash) {
  145.       XmlElement e = this.xmldb.createElement (name: "File");
  146.       e.setAttribute (name: "name", value: name);
  147.       e.setAttribute (name: "hash", value: hash);
  148.  
  149.       XmlElement root     = this.xmldb.getDocumentElement ();
  150.       INTEGER    nentries = this.indexdb.length ();
  151.       if (idx < nentries)
  152.          root.insertBefore (newchild: e, refchild: this.indexdb[idx]);
  153.       else
  154.          root.appendChild (node: e);
  155.  
  156.       this.indexdb.insert (entry: idx, name: name, obj: e);
  157.    }
  158. }
  159.